package com.gitee.neuray.security.modules.param.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gitee.neuray.security.modules.param.dao.EngineItemDao;
import com.gitee.neuray.security.modules.param.entity.EngineItemEntity;
import com.gitee.neuray.security.modules.param.entity.ParamItemEntity;
import com.gitee.neuray.security.modules.param.service.ParamItemService;
import com.gitee.neuray.security.modules.sys.entity.SysUserEntity;
import com.gitee.neuray.security.modules.sys.shiro.ShiroUtils;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.neuray.security.common.utils.PageUtils;
import com.gitee.neuray.security.common.utils.Query;

import com.gitee.neuray.security.modules.param.dao.EngineParamDao;
import com.gitee.neuray.security.modules.param.entity.EngineParamEntity;
import com.gitee.neuray.security.modules.param.service.EngineParamService;

import javax.annotation.Resource;


@Service("engineParamService")
public class EngineParamServiceImpl extends ServiceImpl<EngineParamDao, EngineParamEntity> implements EngineParamService {

    @Resource
    private EngineItemDao engineItemDao;

    @Resource
    private ParamItemService paramItemService;

    @Resource
    private EngineParamDao engineParamDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EngineParamEntity> page = this.page(
                new Query<EngineParamEntity>().getPage(params),
                new QueryWrapper<EngineParamEntity>()
        );

        return new PageUtils(page);
    }
    @Override
    public List<Map> getColumn(String tableName) {
        //String tableName = "engine_i3yYl4OS";
        List<Map> columns = engineItemDao.queryTableColumn(tableName);
        List<Map> jqgrid = new ArrayList<>();
        if(columns!=null && columns.size() != 0){
            columns.stream().forEach( (t) -> {
                String name = t.get("colunmName").toString();
                String label = "";
                boolean hidden = false;
                if(!"id".equals(name) && !"create_time".equals(name) && !"create_user".equals(name)
                        && !"create_name".equals(name) && !"update_time".equals(name)
                        && !"update_user".equals(name) && !"update_name".equals(name) && !"dept_id".equals(name)){
                    String id = name.substring(6);
                    ParamItemEntity p = paramItemService.getById(id);
                    label = p.getParamName();
                }else if("create_time".equals(name)){
                    label = "创建时间";
                }else if("create_name".equals(name)){
                    label = "创建人";
                }else if("update_time".equals(name)){
                    label = "修改时间";
                }else if("update_name".equals(name)){
                    label = "修改人";
                }else{
                    label = name;
                    hidden = true;
                }
                Map item = new HashMap();
                item.put("label",label);
                item.put("name",name);
                item.put("index",name);
                if (hidden){
                    item.put("hidden",true);
                }
                jqgrid.add(item);
                /*{ label: '发动机名称', name: 'name', index: 'name', width: 80 },*/
            });
            Collections.reverse(jqgrid);
        }
        return jqgrid;
    }
    @Override
    public PageUtils queryTable(Map map) {
        String tableName = map.get("tableName").toString();
        //String tableName = "engine_i3yYl4OS";
        int count = engineItemDao.getCountByTable(tableName);
        int page = Integer.parseInt(map.get("page").toString());
        int size = Integer.parseInt(map.get("limit").toString());
        int listSize =count;

        Long deptId = ShiroUtils.getUserEntity().getDeptId();

        List<Map> ret = engineItemDao.getParamByPage(deptId,tableName,(page-1)*size > listSize ? listSize: (page-1)*size, page*size > listSize ? listSize: page*size);
        PageUtils pageRet = new PageUtils(ret,count,Integer.parseInt(map.get("limit").toString()),Integer.parseInt(map.get("page").toString()));
        return pageRet;
    }

    @Override
    public void saveParam(Map map) {
        List<Map> maps = JSONObject.parseArray(JSONObject.toJSONString(map.get("list")),Map.class);
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Map create_time = new HashMap();
        create_time.put("name","create_time");
        create_time.put("ret",new Date());
        Map create_user = new HashMap();
        create_user.put("name","create_user");
        create_user.put("ret",userEntity.getUserId());
        Map create_name = new HashMap();
        create_name.put("name","create_name");
        create_name.put("ret",userEntity.getUsername());
        Map update_time = new HashMap();
        update_time.put("name","update_time");
        update_time.put("ret",new Date());
        Map update_user = new HashMap();
        update_user.put("name","update_user");
        update_user.put("ret",userEntity.getUserId());
        Map update_name = new HashMap();
        update_name.put("name","update_name");
        update_name.put("ret",userEntity.getUsername());
        Map dept_id = new HashMap();
        dept_id.put("name","dept_id");
        dept_id.put("ret",userEntity.getDeptId());
        maps.add(create_time);
        maps.add(create_user);
        maps.add(create_name);
        maps.add(update_time);
        maps.add(update_user);
        maps.add(update_name);
        maps.add(dept_id);
        String tableName = map.get("tableName").toString();
        //String tableName = "engine_i3yYl4OS";
        engineParamDao.saveParam(maps,tableName);

    }

    @Override
    public void updateParam(Map map) {
        List<Map> maps = JSONObject.parseArray(JSONObject.toJSONString(map.get("list")),Map.class);
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Map update_time = new HashMap();
        update_time.put("name","update_time");
        update_time.put("ret",new Date());
        Map update_user = new HashMap();
        update_user.put("name","update_user");
        update_user.put("ret",userEntity.getUserId());
        Map update_name = new HashMap();
        update_name.put("name","update_name");
        update_name.put("ret",userEntity.getUsername());
        maps.add(update_time);
        maps.add(update_user);
        maps.add(update_name);
        String tableName = map.get("tableName").toString();
        //String tableName = "engine_i3yYl4OS";
        Long id =  Long.parseLong(map.get("id").toString());
        engineParamDao.updateParam(maps,tableName,id);

    }

    @Override
    public void deleteParam(Map map) {
        //String tableName = "engine_i3yYl4OS";
        String tableName = map.get("tableName").toString();
        List<Long> id = (List<Long>) map.get("ids");
        engineParamDao.deleteParam(tableName,id);

    }
	@Override
    public Map getExcelTitleById(String id) {
        return engineItemDao.getExcelTitleById(id);
    }

    @Override
    public EngineItemEntity getTableNameById(String id) {
        return engineItemDao.getTableNameById(id);
    }

    @Override
    public List<Map> getParamByTable(String tableName) {
        return engineItemDao.getParamByTable(tableName);
    }
    @Override
    public void saveList(List<String> list, String tableName) {
        engineParamDao.saveList(list,tableName);
    }}
